<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }

    .pa_scbg_ok_t {
        background-color: #254060;
        height: 360px;
        color: white;
        overflow: auto;
    }

    .pa_scbg_ok {
        margin-top: 20px;
        height: 40px;
        line-height: 40px;
        width: 95%;
        margin-left: 3%;
        text-align: center;
    }

    .pa_scbg_ok_rqtm {
        margin-top: 10px;
        height: 40px;
        line-height: 40px;
        width: 70%;
        margin-left: 20%;
        text-align: center;
    }

    .pa_scbg_ok_bgzj {
        margin-top: 5%;
        height: 40px;
        line-height: 40px;
        width: 50%;
        margin-left: 65%;
        text-align: center;
    }


    .pa_scbg_ok_but {
        height: 50px;
        line-height: 50px;
        margin-top: 160%;
        width: 120px;
        font-size: 30px;
        color: white;
        margin-left: -285%;
        text-align: center;
    }

    .pa_scbg_ok_but_close {
        height: 50px;
        line-height: 50px;
        margin-top: 160%;
        width: 120px;
        font-size: 30px;
        color: white;
        margin-left: 30%;
        text-align: center;
    }

    .pa_scbg_ok input {
        height: 40px;
        min-height: 40px;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟久泰</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:20%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:25%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:15%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>合格品报工</p>
            </div>
        </div>
        <div class="aui-flex-col aui-flex-center">
            <div class="aui-flex-item-5 pa_scbg_ok_t">
                <div class="aui-flex-col">
                    <div class="aui-flex-item-12">
                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>工&nbsp&nbsp&nbsp&nbsp单&nbsp&nbsp&nbsp号：</span>
                                    <span id="workOrderCode"></span>
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>物&nbsp&nbsp料&nbsp编&nbsp码：</span>
                                    <span id="mrlCode"></span>
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>物&nbsp&nbsp料&nbsp名&nbsp称：</span>
                                    <span id="mrlName"></span>
                                </div>
                            </div>
                        </div>

                       <!--  <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>可报工数量：</span>
                                    <span id="overageQty"></span>
                                </div>
                            </div>
                        </div> -->
                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>下挂数量：</span>
                                    <span id="hangDownQty"></span>
                                </div>
                            </div>
                        </div>
                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>可报工数量：</span>
                                    <span id="overageQty"></span>
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ok">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    <span>批&nbsp&nbsp&nbsp&nbsp&nbsp次&nbsp&nbsp&nbsp&nbsp号：</span>
                                    <span id="lotCode"></span>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
            </div>
            <div class="aui-flex-item-7 pa_scbg_ok_t">
                <div class="aui-flex-col">
                    <div class="aui-flex-item-12">

                        <div class="aui-flex-col pa_scbg_ok" style="width: 95%;">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    容&nbsp&nbsp器&nbsp条&nbsp码：</div>
                                <div class="aui-list-item-input"
                                    style="background-color: white;float: left;width: 300px;height: 35px;">
                                    <input type="text" id="containerTm" class="aui-input" onclick="getIdSelector();"
                                        onfocus="focusFn(this)" />
                                </div>
                            </div>
                        </div>


                        <table class="table" style='width:90%; margin-left: 5%; margin-top: 3%;' id="table">
                            <thead>
                                <tr style="text-align: center;font-size: 20px;font-weight: bold;">
                                    <td algin='center'>序号</td>
                                    <td algin='center'>容器条码</td>
                                    <td algin='center'>报工数量</td>
                                    <td algin='center'>操作</td>
                                </tr>
                            </thead>
                            <tbody id="tbodyHtml">

                            </tbody>
                        </table>

                        <div class="aui-flex-col pa_scbg_ok_bgzj" style="margin-left: 10%;">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    报工总计：
                                    <div class="aui-list-item-label" style="margin-left: 80%; margin-top: -18%; ">
                                        <input type="text" id="sum" value="" disabled="true"
                                            style="color:white;font-size: 20px;float: left;" />
                                    </div>

                                </div>


                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="aui-flex-col aui-flex-center" style="height: 120px;background-color: #254060;">
            <div class="aui-flex-item-12">
                <div class="aui-flex-col">
                    <div class="pa_scbg_ok_but" style="background-color: #4B7902;margin-top: 5%;margin-left: 2%;"
                        onclick="save()" id="submitButton">
                        提交
                    </div>
                    <div class="pa_scbg_ok_but_close" style="background-color: #D9001B;margin-top: 5%;margin-left: 8%;"
                        onclick="cancle()">
                        关闭
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    //光标所在id
    var idSelector = "";
    var sum = 0;
    var mrlCode = "";//物料编码
    var arrSave = [];//物料编码


    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        doSearch()

        api.setFocus({
            inputId: 'containerTm',
            focus: true
        })
        $("#sum").val(sum);
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    //查询工单信息
    function doSearch() {
        var workOrderCode = api.pageParam.workOrderCode;
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyDDWorkOrder";
        var data = {
            values: {
                workOrderCode: workOrderCode,
                workCenterCode: api.pageParam.workCenterCode,
                state: "开工",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == 0) {

                    var list = eval(myobj.list);
                    if (list != null && list.length > 0) {
                        document.getElementById("workOrderCode").innerHTML = list[0].workOrderCode;
                        document.getElementById("mrlCode").innerHTML = list[0].mrlCode;
                        document.getElementById("mrlName").innerHTML = list[0].mrlName;
                        document.getElementById("lotCode").innerHTML = list[0].lotCode;
                        document.getElementById("overageQty").innerHTML = list[0].qualifyQty - list[0].bgQty;  //下挂合格数-合格品已报工数
                        document.getElementById("hangDownQty").innerHTML = list[0].hangDownQty;  //下挂数量
                       
                        mrlCode = list[0].mrlCode;
                    } else {
                        document.getElementById("workOrderCode").innerHTML = '';
                        document.getElementById("mrlCode").innerHTML = '';
                        document.getElementById("mrlName").innerHTML = '';
                        document.getElementById("lotCode").innerHTML = '';
                        //document.getElementById("overageQty").innerHTML = '';
                        document.getElementById("overageQty").innerHTML = '';
                        document.getElementById("hangDownQty").innerHTML = '';
                        api.toast({
                            msg: "工单【" + workOrderCode + "】开工信息不存在！",
                            duration: 3000
                        });
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //保存
    function save() {

         if (!clickThrottle()) {
            return;
        }
        var workOrderCode = document.getElementById("workOrderCode").innerHTML;
        var overageQty = parseInt(document.getElementById("overageQty").innerHTML);
        var containerTm = $("#containerTm").val();
        // var qty = $("#qty").val();

        //工单号非空校验
        if (workOrderCode == null || workOrderCode == "" || workOrderCode == undefined) {
            api.toast({
                msg: '工单号不能为空！',
                duration: 3000
            });
            return;
        }



        if (arrSave == null || arrSave == "" || arrSave.length == 0) {
            api.toast({
                msg: '请扫描/录入容器条码！',
                duration: 3000
            });
            return;
        }


        if (parseInt(sum) > overageQty) {
            api.toast({
                msg: '当前报工总数【' + sum + '】,大于可报工数量【' + overageQty + '】！',
                duration: 3000
            });
            return;
        }


        for (var i = 0; i < arrSave.length; i++) {
            //数量为正整数
            if (!new RegExp("^[0-9]*[1-9][0-9]*$").test(arrSave[i].qty)) {
                api.toast({
                    msg: '数量应为正整数,请检查！',
                    duration: 3000
                });
                return;
            }
        }

        const submitButton = document.getElementById('submitButton');
        submitButton.style.pointerEvents = "none"; // 禁用提交按钮

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyReportSave2";
        var data = {
            values: {
                workOrderCode: workOrderCode,
                workCellCode: api.pageParam.workCellCode,
                containerTm: containerTm,
                //               qty: qty,
                list: JSON.stringify(arrSave),
                sum: sum,
                type: "0", //0合格 1不合格  
                workCenterCode: api.pageParam.workCenterCode,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });
                    $("#containerTm").val("");
                    $("#qty").val("");
                    doSearch();
                    arrSave = [];
                    sum = 0;
                    selectTable(arrSave);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    $("#containerTm").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            console.log(tm);
            if (tm != null && tm != "" && tm != undefined) {
                $("#containerTm").blur();
                api.setFocus({
                    inputId: 'containerTm',
                    focus: true
                })
            }
            // 查询归档标识   1：未归档；0已归档
            queryGDFlag(tm);

            
            $("#containerTm").val("");
        }
    });

    //取消按钮
    function cancle() {
        api.closeWin({});
        api.execScript({
            name: 'productionAssemblyScbg-dd',
            script: 'refresh()'
        });
    }

    function removerqtm(rqtm) {
        for (var i = 0; i < arrSave.length; i++) {
            if (rqtm == arrSave[i].rqtm) {
                arrSave.splice(i, 1)
            }
        }
        selectTable(arrSave);

    }

    // 根据容器条码查找报工数量
    function addArr(rqtm) {
        //判断箱号是否重复
        for (var i = 0; i < arrSave.length; i++) {
            if (rqtm == arrSave[i].rqtm) {
                api.toast({
                    msg: '容器条码已存在！',
                    duration: 3000
                });
                return;
            }
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/selectByRqtm";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                rqtm: rqtm,
                mrlCode: mrlCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var qty = myobj.qty
                    var model = {
                        rqtm: rqtm, //容器条码
                        qty: qty, //数量
                        listqty: qty, //数量
                    }
                    arrSave.push(model);
                    selectTable(arrSave);
                    var list = myobj.list;

                } else if (myobj.code == '1') {
                    api.setFocus({
                    inputId: 'containerTm',
                    focus: true
                    })
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $api.html($api.byId("tbodyHtml"), "");
                }
            }
        })
    }

    function selectTable(list) {
        sum = 0;
        var html = "";
        for (var i = 0; i < list.length; i++) {
            x = i + 1;
            var bgnumId = "bgnum" + i;
            var ext02Id = "ext02" + i;
            html += "<tr style='text-align: center;color: white;font-size: 20px;'>";
            html += "<td name='no' width = '8%'>" + x + "</td>";
            html += "<td name='rqtm' width = '29%'>" + list[i].rqtm + "</td>";
            html += "<td  name='bgnum' width = '19%'><input type='number' id=\"" + bgnumId + "\"  onblur='changeBgnum(\"" + bgnumId + "\",\"" + list[i].listqty + "\" );' style='color:black;background-color:white; height:30px; min-height:50px;' value=\"" + list[i].qty + "\" /></td>";
            html += "<td name='ext02'  width = '19%'><button type='number' id=\"" + ext02Id + "\" onclick='removerqtm(\"" + list[i].rqtm + "\" );' style='white:black;background-color:red; height:30px; min-height:50px;' value=\"" + x + "\" />" + "移除" + "</td>";

            html += "</tr>";

            sum = sum + parseInt(list[i].qty);
        }

        $api.html($api.byId("tbodyHtml"), html);
        if (list.length == 0) {
            sum = 0;
        }
        $("#sum").val(sum);
        $("#containerTm").select();
    }
    function changeBgnum(bgnumId, listqty) {
        sum = 0;
        var qty = document.getElementById(bgnumId).value;
        if (parseInt(qty) > parseInt(listqty)) {
            api.toast({
                msg: '不能大于容器可用容量【'+listqty+'】！',
                duration: 3000
            });
            selectTable(arrSave);
        } else {
            for (var i = 0; i < arrSave.length; i++) {
                arrSave[i].qty = document.getElementById("bgnum" + i).value;
                if(arrSave[i].qty===''||arrSave[i].qty===null||arrSave[i].qty===undefined){
                    arrSave[i].qty=0
                }
                sum = sum + parseInt(arrSave[i].qty);
            }
            $("#sum").val(sum);
        }

    }

    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };



    //查询归档标识   1：未归档；0已归档
    function queryGDFlag(rqtm) {
        //判断容器条码是否重复
        for (var i = 0; i < arrSave.length; i++) {
            if (rqtm == arrSave[i].rqtm) {
                api.toast({
                    msg: '容器条码已存在！',
                    duration: 3000
                });
                return;
            }
        }
        //查询归档弹框确认标识
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/selectByRqtmGDFlag";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                rqtm: rqtm, //容器条码
                workCellCode: api.pageParam.workCellCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    var flag = myobj.flag;

                    console.log('容器条码归档标识：' + flag);

                    //标识为1：未归档，弹出确认框
                    if (flag == "1") {
                        $("#containerTm").val("");
                        api.toast({
                            msg: myobj.messageGD,
                            duration: 3000
                        });
                        /* var workCenterCodeMM = $("#workCenterCode").val();
                        var workCellCodeMM = $("#workCellCode").val();
                        api.confirm({
                            title: '确认报工',
                            msg: "容器【"+ rqtm +"】处于未归档状态，是否归档并继续报工？",
                            buttons: ['确定', '取消']
                        }, function (ret, err) {
                            var index = ret.buttonIndex;
                            if (index == 1) {

                                console.log('点击了确定按钮');

                                // 进行归档操作
                                saveGD(rqtm);
                                
                            }else{
                                console.log('点击了取消按钮');

                                $("#containerTm").val("");

                                api.setFocus({
                                    inputId: 'containerTm',
                                    focus: true
                                })
                            }
                        }); */

                        //标识为0，已归档，不弹框
                    } else {

                        // 根据容器条码查找报工数量
                        addArr(rqtm);
                    }

                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#containerTm").val("");

                    api.setFocus({
                        inputId: 'containerTm',
                        focus: true
                    })


                }
            }
        });


    }

    // 保存容器条码归档
    function saveGD(rqtm) {
        
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveByRqtmGD";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                rqtm: rqtm, //容器条码
                mrlCode: mrlCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    
                    // 根据容器条码查找报工数量
                    addArr(rqtm);

                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });


    }


    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }


</script>

</html>